﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;
using MvcPaging;

namespace zichan.BLL
{
   public class ServerPurposeSubClassService
    {
        public static IPagedList<ServerPurposeSubClass> ServerPurposeSubClassList(string name,int purposeId, int pageSize, int pageIndex)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<ServerPurposeSubClass> spc = db.ServerPurposeSubClasses.Where(o=>o.serverPurposeId==purposeId);

                if (!string.IsNullOrEmpty(name))
                {
                    spc = spc.Where(o => o.serverPurposeSubName == name);
                }
                List<ServerPurposeSubClass> list = spc.OrderBy(o => o.sort).ThenBy(o => o.serverPurposeId).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
                foreach (ServerPurposeSubClass s in list)
                {
                    s.UseCount = db.PutawayServers.Count(o => o.serverPurposeId == s.serverPurposeId);
                }
                return list.ToPagedList(pageIndex - 1, pageSize, spc.Count());
            }
        }
        public static string ServerPurposeAdd(ServerPurposeSubClass model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    if (db.ServerPurposeSubClasses.Count(o =>o.serverPurposeId==model.serverPurposeId && o.serverPurposeSubName == model.serverPurposeSubName) > 0)
                        return "该用途已存在，无法反复添加";
                    db.ServerPurposeSubClasses.Add(model);
                    db.SaveChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "无法添加";
                }
            }
        }
        public static string ServerPurposeEdit(ServerPurposeSubClass model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    if (db.ServerPurposeSubClasses.Count(o => o.serverPurposeId == model.serverPurposeId &&o.serverPurposeSubId!=model.serverPurposeSubId&& o.serverPurposeSubName == model.serverPurposeSubName) > 0)
                        return "该用途已存在";
                    db.Entry(model).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "无法编辑";
                }
            }
        }
        public static string ServerPurposeDelete(int id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    ServerPurposeSubClass m = db.ServerPurposeSubClasses.FirstOrDefault(o => o.serverPurposeSubId== id);
                    if (m != null)
                    {
                        if (db.PutawayServers.Count(o => o.serverPurposeSubId == id) > 0)
                        {
                            return m.serverPurposeSubName + "不能删除，已在使用中";
                        }
                        db.ServerPurposeSubClasses.Remove(m);
                        db.SaveChanges();
                        //ClassLogService.AddLog("机柜用途分类", "删除" + "\"" + m.serverPurposeSubName + "\"");
                    }
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return "无法删除";
                }
            }
        }
        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <param name="Id">serverPurposeSubId</param>
        /// <returns></returns>
        public static ServerPurposeSubClass Get(int Id)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.ServerPurposeSubClasses.FirstOrDefault(o => o.serverPurposeSubId == Id);
            }
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="nameList">输入的名称集合</param>
        /// <param name="existName">重复的名称</param>
        /// <param name="nameListForLog">保存入数据库的名称，用以做日志使用</param>
        /// <returns></returns>
        public static string AddBatchClass(List<string> nameList, out List<string> existName, out List<string> nameListForLog,int purposeId)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                try
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    int maxSort =db.ServerPurposeSubClasses.Count()==0?0:db.ServerPurposeSubClasses.Max(o => o.sort);
                    List<ServerPurposeSubClass> classList = new List<ServerPurposeSubClass>();
                    foreach (string n in nameList)
                    {
                        if (db.ServerPurposeSubClasses.Count(o => o.serverPurposeSubName == n) > 0 || classList.Count(o => o.serverPurposeSubName == n) > 0)
                        {
                            existName.Add(n);
                            continue;
                        }
                        nameListForLog.Add(n);
                        ServerPurposeSubClass model = new ServerPurposeSubClass();
                        maxSort += 1;
                        model.serverPurposeSubName = n;
                        model.sort = maxSort;
                        model.serverPurposeId = purposeId;
                        classList.Add(model);
                    }
                    if (classList.Count() > 0)
                    {
                        db.ServerPurposeSubClasses.AddRange(classList);
                        db.SaveChanges();
                    }
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    existName = new List<string>();
                    nameListForLog = new List<string>();
                    return "无法添加，请重试";
                }
            }
        }
        public static int MaxSort()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                if (db.ServerPurposeSubClasses.Count() > 0)
                    return db.ServerPurposeSubClasses.Max(o => o.sort);
                else return 0;
            }
        }
        public static List<ServerPurposeSubClass> GetList(int purposeId)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.ServerPurposeSubClasses.Where(o=>o.serverPurposeId==purposeId).ToList();
            }
        }
        public static List<ServerPurposeSubClass> GetList()
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                return db.ServerPurposeSubClasses.ToList();
            }
        }
    }
}
